package com.sk.cardeal.act.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.ChangeListAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.ByPartsEntity;
import com.sk.cardeal.entity.CanChangeList;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.view.MultiStateView;
import com.sk.cardeal.view.SimpleMultiStateView;
import com.sk.cardeal.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by sk on 2018/4/16.
 */

public class CanChangeListActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.SimpleMultiStateView)
    SimpleMultiStateView simpleMultiStateView;
    private String carCode = "";
    private String partsId = "";
    private ChangeListAdapter adapter;
    private List<ByPartsEntity> allDataList = new ArrayList<>();
    private int groupPos,childPos;

    @Override
    protected int getContentView() {
        return R.layout.activity_change_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setMyCallBack(this);
        carCode = getIntent().getStringExtra("carCode");
        partsId = getIntent().getStringExtra("partsId");
        groupPos = getIntent().getIntExtra("groupPos",0);
        childPos = getIntent().getIntExtra("childPos",0);

        initStateView();

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ChangeListAdapter(R.layout.item_change_list,allDataList);
        dataRyc.setAdapter(adapter);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, null);
        TextView textView = emptyView.findViewById(R.id.content);
        textView.setText("暂无可更换的产品!");
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        AppApi.getChangeList(carCode,partsId,"1",this, 0,this);
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                finish();
                break;
        }
    }

    private void initStateView() {
        if (simpleMultiStateView == null) return;
        simpleMultiStateView.setEmptyResource(R.layout.view_empty)
                .setRetryResource(R.layout.view_retry)
                .setLoadingResource(R.layout.view_loading)
                .setNoNetResource(R.layout.view_nonet)
                .build()
                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
                    @Override
                    public void onReload() {
                    }
                });
    }

    public void showLoading() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showLoadingView();
        }
    }

    public void showSuccess() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showContent();
        }
    }

    public void showFaild() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showErrorView();
        }
    }

    public void showNoNet() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showNoNetView();
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        showSuccess();
        CanChangeList canChangeList = JSON.parseObject(result,CanChangeList.class);
        List<ByPartsEntity> dataList = canChangeList.getResultList();
        allDataList.addAll(dataList);

    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        Intent intent = new Intent();
        intent.putExtra("groupPos",groupPos);
        intent.putExtra("childPos",childPos);
        intent.putExtra("data",allDataList.get(position));
        setResult(RESULT_OK,intent);
        finish();

    }
}
